/*
select * from fn_yielded_cost_report() where lotNumber in ( 'NTL01.00', 'TTXHL.01' )

select distinct part_number from product where p_marketing_part_number is not null
select processStep_id from product where part_number = 'TEST-TX-12' and p_marketing_part_number is not null order by processStep_id desc
select * from fn_get_process_flow_backward_by_id(1439) order by temp_count desc
select * from fn_yielded_cost_report() where processStepID = 1433

select * from fn_get_process_flow_by_id(1439)

EXEC SP_YIELDED_COST_SUMMARY 'COST_REPORT', NULL, NULL, 7
*/

ALTER      PROC SP_YIELDED_COST_SUMMARY
(
 @FG_PART_NUMBER VARCHAR(50),
 @LOTS_DATE_OUT_FROM VARCHAR(20),
 @LOTS_DATE_OUT_TO VARCHAR(20),
 @STEP_SIZE INT
)

AS
BEGIN
	-- STEP 0
	-- INITIALIZATION
	-- ADDED BY BASHA (06/22/2004)
	IF @STEP_SIZE = 0
	BEGIN
		SET @STEP_SIZE = NULL
	END

	IF @LOTS_DATE_OUT_FROM IS NULL OR @LOTS_DATE_OUT_FROM = '' OR @LOTS_DATE_OUT_FROM = '0'
	BEGIN
		SELECT @LOTS_DATE_OUT_FROM = '01/01/1900'
	END

	IF @LOTS_DATE_OUT_TO IS NULL OR @LOTS_DATE_OUT_TO = '' OR @LOTS_DATE_OUT_TO = '0'
	BEGIN
		SELECT @LOTS_DATE_OUT_TO = '01/01/9999'
	END

	DECLARE @FG_PROCESS_STEP_ID INT
	SELECT @FG_PROCESS_STEP_ID = PROCESSSTEP_ID FROM PRODUCT WHERE PART_NUMBER = @FG_PART_NUMBER AND P_MARKETING_PART_NUMBER IS NOT NULL

	DECLARE @FLOW_PROCESS_STEP_ID INT
	DECLARE @FLOW_PROCESS_STEP_TYPE_NAME VARCHAR(20)

	DECLARE @DATE_OUT_START DATETIME
	DECLARE @DATE_OUT_END DATETIME
	DECLARE @DATE_OUT_START_ITERATOR DATETIME

	/*SELECT @DATE_OUT_START = MIN( CAST ( DATEOUT AS DATETIME ) ), @DATE_OUT_END = MAX( CAST( DATEOUT AS DATETIME ) ) 
		FROM FN_YIELD_COST_REPORT() 
		WHERE DATEOUT >= CAST ( @LOTS_DATE_OUT_FROM AS DATETIME )
		AND DATEOUT <= CAST ( @LOTS_DATE_OUT_TO AS DATETIME ) */

	DECLARE @GROSS_DIE_PER_WAFER INT
	DECLARE @PS_FLOW_CURSOR CURSOR

	DECLARE @TOTAL_UNITS_PER_STEP INT
	DECLARE @FAB_TOTAL_WAFERS_PER_STEP INT
	DECLARE @FAB_TOTAL_UNITS_PER_STEP INT
	DECLARE @SORT_TOTAL_UNITS_PER_STEP INT
	DECLARE @ASSY_TOTAL_UNITS_PER_STEP INT
	DECLARE @TEST_TOTAL_UNITS_PER_STEP INT

	DECLARE @FAB_STD_CUM_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @SORT_STD_CUM_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @ASSY_STD_CUM_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @TEST_STD_CUM_UNIT_COST_PER_STEP NUMERIC(9,2)

	DECLARE @FAB_VND_CUM_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @SORT_VND_CUM_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @ASSY_VND_CUM_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @TEST_VND_CUM_UNIT_COST_PER_STEP NUMERIC(9,2)

	DECLARE @TOTAL_STD_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @FAB_STD_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @SORT_STD_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @ASSY_STD_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @TEST_STD_UNIT_COST_PER_STEP NUMERIC(9,2)

	DECLARE @TOTAL_VND_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @FAB_VND_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @SORT_VND_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @ASSY_VND_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @TEST_VND_UNIT_COST_PER_STEP NUMERIC(9,2)

	DECLARE @TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @FAB_FINISHED_PART_STD_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @SORT_FINISHED_PART_STD_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @ASSY_FINISHED_PART_STD_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @TEST_FINISHED_PART_STD_UNIT_COST_PER_STEP NUMERIC(9,2)

	DECLARE @TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @FAB_FINISHED_PART_VND_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @SORT_FINISHED_PART_VND_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @ASSY_FINISHED_PART_VND_UNIT_COST_PER_STEP NUMERIC(9,2)
	DECLARE @TEST_FINISHED_PART_VND_UNIT_COST_PER_STEP NUMERIC(9,2)

	
	--#############################################
	--###### RESULTS TABLE DECLARATION START ######
	--#############################################

	DECLARE @RESULTS TABLE( STEP_FIRST_DAY DATETIME, 

				FAB_TOTAL_WAFERS_PER_STEP INT, FAB_TOTAL_UNITS_PER_STEP INT, SORT_TOTAL_UNITS_PER_STEP INT, 
				ASSY_TOTAL_UNITS_PER_STEP INT, TEST_TOTAL_UNITS_PER_STEP INT, TOTAL_UNITS_PER_STEP INT, 
				
				FAB_STD_UNIT_COST_PER_STEP NUMERIC(9,2), SORT_STD_UNIT_COST_PER_STEP NUMERIC(9,2), 
				ASSY_STD_UNIT_COST_PER_STEP NUMERIC(9,2), TEST_STD_UNIT_COST_PER_STEP NUMERIC(9,2), 
				TOTAL_STD_UNIT_COST_PER_STEP NUMERIC(9,2), 

				FAB_STD_CUM_UNIT_COST_PER_STEP NUMERIC(9,2), SORT_STD_CUM_UNIT_COST_PER_STEP NUMERIC(9,2), 
				ASSY_STD_CUM_UNIT_COST_PER_STEP NUMERIC(9,2), TEST_STD_CUM_UNIT_COST_PER_STEP NUMERIC(9,2), 

				FAB_FINISHED_PART_STD_UNIT_COST_PER_STEP NUMERIC(9,2), SORT_FINISHED_PART_STD_UNIT_COST_PER_STEP NUMERIC(9,2),
				ASSY_FINISHED_PART_STD_UNIT_COST_PER_STEP NUMERIC(9,2), TEST_FINISHED_PART_STD_UNIT_COST_PER_STEP NUMERIC(9,2),
				TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP NUMERIC(9,2),

				FAB_VND_UNIT_COST_PER_STEP NUMERIC(9,2), 
				SORT_VND_UNIT_COST_PER_STEP NUMERIC(9,2), ASSY_VND_UNIT_COST_PER_STEP NUMERIC(9,2), 
				TEST_VND_UNIT_COST_PER_STEP NUMERIC(9,2), TOTAL_VND_UNIT_COST_PER_STEP NUMERIC(9,2),

				FAB_VND_CUM_UNIT_COST_PER_STEP NUMERIC(9,2), SORT_VND_CUM_UNIT_COST_PER_STEP NUMERIC(9,2), 
				ASSY_VND_CUM_UNIT_COST_PER_STEP NUMERIC(9,2), TEST_VND_CUM_UNIT_COST_PER_STEP NUMERIC(9,2), 

				FAB_FINISHED_PART_VND_UNIT_COST_PER_STEP NUMERIC(9,2), SORT_FINISHED_PART_VND_UNIT_COST_PER_STEP NUMERIC(9,2),
				ASSY_FINISHED_PART_VND_UNIT_COST_PER_STEP NUMERIC(9,2), TEST_FINISHED_PART_VND_UNIT_COST_PER_STEP NUMERIC(9,2),
				TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP NUMERIC(9,2) )

	--###########################################
	--###### RESULTS TABLE DECLARATION END ######
	--###########################################


	/*-------------------------------------------------------------------------------*/
	/*------------------   LOAD DATA FROM FUNCTION TO A TEMP TABLE   ----------------*/
	/*-------------------------------------------------------------------------------*/

	-- LOAD PROCESS_FLOW INFORMATION FN_GET_PROCESS_FLOW_BACKWARD_BY_ID INTO A TEMP TABLE
	DECLARE @PROCESS_FLOW_INFO_TABLE
		TABLE ( PROCESSSTEP_ID INT, PROCESS_TYPE_NAME VARCHAR(50) )

	--INSERT @PROCESS_FLOW_INFO_TABLE SELECT PROCESSSTEP_ID, PROCESS_TYPE_NAME FROM FN_GET_PROCESS_FLOW_BACKWARD_BY_ID( @FG_PROCESS_STEP_ID ) ORDER BY TEMP_COUNT DESC
	INSERT @PROCESS_FLOW_INFO_TABLE SELECT PROCESSSTEP_ID, PROCESS_TYPE_NAME FROM FN_GET_PROCESS_FLOW_BACKWARD_BY_ID( @FG_PROCESS_STEP_ID ) ORDER BY TEMP_COUNT DESC

	-- LOAD THE DATA FROM FN_YIELDED_COST_REPORT FUNCTION INTO A TEMP TABLE
	DECLARE @YIELD_COST_TABLE 
		TABLE( PROCESSSTEPID INT, LOTNUMBER VARCHAR(20), PRODUCTNAME VARCHAR(30), DATEIN VARCHAR(30), 
                DATEOUT VARCHAR(30), QTYIN INT, QTYOUT INT, STEP_YIELD NUMERIC(9,2), TOTAL_YIELD NUMERIC(9,2), 
                STD_UNIT_COST NUMERIC(9,2), STD_CUM_UNIT_COST NUMERIC(9,2), STD_FINISHED_UNIT_COST NUMERIC(9,2), 
                VND_UNIT_COST NUMERIC(9,2), VND_CUM_UNIT_COST NUMERIC(9,2), VND_FINISHED_UNIT_COST NUMERIC(9,2), 
                STAGE INT, VENDORLOC VARCHAR(30) )

	INSERT @YIELD_COST_TABLE SELECT * FROM FN_YIELD_COST_REPORT()

	SELECT @DATE_OUT_START = MIN( CAST ( DATEOUT AS DATETIME ) ), @DATE_OUT_END = MAX( CAST( DATEOUT AS DATETIME ) ) 
		FROM @YIELD_COST_TABLE 
		WHERE DATEOUT >= CAST ( @LOTS_DATE_OUT_FROM AS DATETIME )
		AND DATEOUT <= CAST ( @LOTS_DATE_OUT_TO AS DATETIME ) 
		AND PROCESSSTEPID IN ( SELECT PROCESSSTEP_ID FROM @PROCESS_FLOW_INFO_TABLE )

	/*-------------------------------------------------------------------------------*/
	/*----------------------- LOOP THRU EACH WEEK -----------------------------------*/
	/*-------------------------------------------------------------------------------*/
	SELECT @DATE_OUT_START_ITERATOR = @DATE_OUT_START

	WHILE @DATE_OUT_START_ITERATOR <= @DATE_OUT_END
	BEGIN

		/**********  RESET ALL VARIABLES FOR EACH WEEK START ***********/
		SELECT @TOTAL_UNITS_PER_STEP = 0
		SELECT @FAB_TOTAL_WAFERS_PER_STEP = 0
		SELECT @FAB_TOTAL_UNITS_PER_STEP = 0
		SELECT @SORT_TOTAL_UNITS_PER_STEP = 0
		SELECT @ASSY_TOTAL_UNITS_PER_STEP = 0
		SELECT @TEST_TOTAL_UNITS_PER_STEP = 0
		
		SELECT @FAB_STD_CUM_UNIT_COST_PER_STEP = 0.0
		SELECT @SORT_STD_CUM_UNIT_COST_PER_STEP = 0.0
		SELECT @ASSY_STD_CUM_UNIT_COST_PER_STEP = 0.0
		SELECT @TEST_STD_CUM_UNIT_COST_PER_STEP = 0.0
		
		SELECT @FAB_VND_CUM_UNIT_COST_PER_STEP = 0.0		
		SELECT @SORT_VND_CUM_UNIT_COST_PER_STEP = 0.0
		SELECT @ASSY_VND_CUM_UNIT_COST_PER_STEP = 0.0
		SELECT @TEST_VND_CUM_UNIT_COST_PER_STEP = 0.0
		
		SELECT @TOTAL_STD_UNIT_COST_PER_STEP = 0.0
		SELECT @FAB_STD_UNIT_COST_PER_STEP = 0.0
		SELECT @SORT_STD_UNIT_COST_PER_STEP = 0.0
		SELECT @ASSY_STD_UNIT_COST_PER_STEP = 0.0
		SELECT @TEST_STD_UNIT_COST_PER_STEP = 0.0

		SELECT @TOTAL_VND_UNIT_COST_PER_STEP = 0.0
		SELECT @FAB_VND_UNIT_COST_PER_STEP = 0.0
		SELECT @SORT_VND_UNIT_COST_PER_STEP = 0.0
		SELECT @ASSY_VND_UNIT_COST_PER_STEP = 0.0
		SELECT @TEST_VND_UNIT_COST_PER_STEP = 0.0
		
		SELECT @TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP = 0.0
		SELECT @FAB_FINISHED_PART_STD_UNIT_COST_PER_STEP = 0.0
		SELECT @SORT_FINISHED_PART_STD_UNIT_COST_PER_STEP = 0.0
		SELECT @ASSY_FINISHED_PART_STD_UNIT_COST_PER_STEP = 0.0
		SELECT @TEST_FINISHED_PART_STD_UNIT_COST_PER_STEP = 0.0
		
		SELECT @TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP = 0.0
		SELECT @FAB_FINISHED_PART_VND_UNIT_COST_PER_STEP = 0.0
		SELECT @SORT_FINISHED_PART_VND_UNIT_COST_PER_STEP = 0.0
		SELECT @ASSY_FINISHED_PART_VND_UNIT_COST_PER_STEP = 0.0
		SELECT @TEST_FINISHED_PART_VND_UNIT_COST_PER_STEP = 0.0
		/***********  RESET ALL VARIABLES FOR EACH WEEK END **************/


		/*-------------------------------------------------------------------------------*/
		/*----------------------- LOOP THRU EACH STEP -----------------------------------*/
		/*-------------------------------------------------------------------------------*/
		SET @PS_FLOW_CURSOR = CURSOR FOR
		SELECT PROCESSSTEP_ID, PROCESS_TYPE_NAME FROM @PROCESS_FLOW_INFO_TABLE

		OPEN @PS_FLOW_CURSOR 
	    	FETCH NEXT FROM @PS_FLOW_CURSOR INTO @FLOW_PROCESS_STEP_ID, @FLOW_PROCESS_STEP_TYPE_NAME
	
	        WHILE @@FETCH_STATUS = 0
	        BEGIN

			IF @FLOW_PROCESS_STEP_TYPE_NAME = 'FAB'
			BEGIN
				SELECT @GROSS_DIE_PER_WAFER = PARAMETER_VALUE FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @FLOW_PROCESS_STEP_ID AND PARAMETER_KEY = 'GROSS DIE/WAFER'

				SELECT  @FAB_TOTAL_WAFERS_PER_STEP = SUM( QTYOUT ), 
					@FAB_STD_UNIT_COST_PER_STEP = AVG( STD_UNIT_COST ),
					@FAB_VND_UNIT_COST_PER_STEP = AVG( VND_UNIT_COST ),
					@FAB_STD_CUM_UNIT_COST_PER_STEP = AVG( STD_CUM_UNIT_COST ),
					@FAB_VND_CUM_UNIT_COST_PER_STEP = AVG( VND_CUM_UNIT_COST ),
					@FAB_FINISHED_PART_STD_UNIT_COST_PER_STEP = AVG( STD_FINISHED_UNIT_COST ), 
					@FAB_FINISHED_PART_VND_UNIT_COST_PER_STEP = AVG( VND_FINISHED_UNIT_COST )
					FROM @YIELD_COST_TABLE
					--FROM FN_YIELD_COST_REPORT() 
					WHERE PROCESSSTEPID = @FLOW_PROCESS_STEP_ID
				 	AND DATEOUT >= @DATE_OUT_START_ITERATOR
				 	AND DATEOUT < @DATE_OUT_START_ITERATOR + @STEP_SIZE

				IF @FAB_TOTAL_WAFERS_PER_STEP IS NOT NULL
				BEGIN
					-- WAFER TO DIE COUNT CONVERSION
					SELECT @FAB_TOTAL_UNITS_PER_STEP = @FAB_TOTAL_WAFERS_PER_STEP * @GROSS_DIE_PER_WAFER
					
					-- SET TOTALS
					SELECT @TOTAL_UNITS_PER_STEP = @TOTAL_UNITS_PER_STEP + @FAB_TOTAL_UNITS_PER_STEP	
					SELECT @TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP = @TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP + @FAB_FINISHED_PART_STD_UNIT_COST_PER_STEP
					SELECT @TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP = @TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP + @FAB_FINISHED_PART_VND_UNIT_COST_PER_STEP
					SELECT @TOTAL_STD_UNIT_COST_PER_STEP = @TOTAL_STD_UNIT_COST_PER_STEP + @FAB_STD_UNIT_COST_PER_STEP
					SELECT @TOTAL_VND_UNIT_COST_PER_STEP = @TOTAL_VND_UNIT_COST_PER_STEP + @FAB_VND_UNIT_COST_PER_STEP
				END
			END
	
			ELSE IF @FLOW_PROCESS_STEP_TYPE_NAME = 'SORT'
			BEGIN

				SELECT  @SORT_TOTAL_UNITS_PER_STEP = SUM( QTYOUT ), 
					@SORT_STD_UNIT_COST_PER_STEP = AVG( STD_UNIT_COST ),
					@SORT_VND_UNIT_COST_PER_STEP = AVG( VND_UNIT_COST ),
					@SORT_STD_CUM_UNIT_COST_PER_STEP = AVG( STD_CUM_UNIT_COST ),
					@SORT_VND_CUM_UNIT_COST_PER_STEP = AVG( VND_CUM_UNIT_COST ),
					@SORT_FINISHED_PART_STD_UNIT_COST_PER_STEP = AVG( STD_FINISHED_UNIT_COST ),
					@SORT_FINISHED_PART_VND_UNIT_COST_PER_STEP = AVG( VND_FINISHED_UNIT_COST )

					FROM @YIELD_COST_TABLE 
					WHERE PROCESSSTEPID = @FLOW_PROCESS_STEP_ID
				 	AND DATEOUT >= @DATE_OUT_START_ITERATOR	AND DATEOUT < @DATE_OUT_START_ITERATOR + @STEP_SIZE

				IF @SORT_TOTAL_UNITS_PER_STEP IS NOT NULL
				BEGIN
					-- SET TOTALS	
					SELECT @TOTAL_UNITS_PER_STEP = @TOTAL_UNITS_PER_STEP + @SORT_TOTAL_UNITS_PER_STEP
					SELECT @TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP = @TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP + @SORT_FINISHED_PART_STD_UNIT_COST_PER_STEP
					SELECT @TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP = @TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP + @SORT_FINISHED_PART_VND_UNIT_COST_PER_STEP
					SELECT @TOTAL_STD_UNIT_COST_PER_STEP = @TOTAL_STD_UNIT_COST_PER_STEP + @SORT_STD_UNIT_COST_PER_STEP
					SELECT @TOTAL_VND_UNIT_COST_PER_STEP = @TOTAL_VND_UNIT_COST_PER_STEP + @SORT_VND_UNIT_COST_PER_STEP
				END
			END
	
			ELSE IF @FLOW_PROCESS_STEP_TYPE_NAME = 'Assembly'
			BEGIN

				SELECT  @ASSY_TOTAL_UNITS_PER_STEP = SUM( QTYOUT ), 
					@ASSY_STD_UNIT_COST_PER_STEP = AVG( STD_UNIT_COST ),
					@ASSY_VND_UNIT_COST_PER_STEP = AVG( VND_UNIT_COST ),
					@ASSY_STD_CUM_UNIT_COST_PER_STEP = AVG( STD_CUM_UNIT_COST ),
					@ASSY_VND_CUM_UNIT_COST_PER_STEP = AVG( VND_CUM_UNIT_COST ),
					@ASSY_FINISHED_PART_STD_UNIT_COST_PER_STEP = AVG( STD_FINISHED_UNIT_COST ),
					@ASSY_FINISHED_PART_VND_UNIT_COST_PER_STEP = AVG( VND_FINISHED_UNIT_COST )
					FROM @YIELD_COST_TABLE
					--FROM FN_YIELD_COST_REPORT() 
					WHERE PROCESSSTEPID = @FLOW_PROCESS_STEP_ID
				 	AND DATEOUT >= @DATE_OUT_START_ITERATOR
				 	AND DATEOUT < @DATE_OUT_START_ITERATOR + @STEP_SIZE

				IF @ASSY_TOTAL_UNITS_PER_STEP IS NOT NULL
				BEGIN
					-- SET TOTALS
					SELECT @TOTAL_UNITS_PER_STEP = @TOTAL_UNITS_PER_STEP + @ASSY_TOTAL_UNITS_PER_STEP	
					SELECT @TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP = @TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP + @ASSY_FINISHED_PART_STD_UNIT_COST_PER_STEP
					SELECT @TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP = @TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP + @ASSY_FINISHED_PART_VND_UNIT_COST_PER_STEP
					SELECT @TOTAL_STD_UNIT_COST_PER_STEP = @TOTAL_STD_UNIT_COST_PER_STEP + @ASSY_STD_UNIT_COST_PER_STEP
					SELECT @TOTAL_VND_UNIT_COST_PER_STEP = @TOTAL_VND_UNIT_COST_PER_STEP + @ASSY_VND_UNIT_COST_PER_STEP
				END	
			END
	
			ELSE IF @FLOW_PROCESS_STEP_TYPE_NAME = 'TEST'
			BEGIN
				SELECT  @TEST_TOTAL_UNITS_PER_STEP = SUM( QTYOUT ), 
					@TEST_STD_UNIT_COST_PER_STEP = AVG( STD_UNIT_COST ),
					@TEST_VND_UNIT_COST_PER_STEP = AVG( VND_UNIT_COST ),
					@TEST_STD_CUM_UNIT_COST_PER_STEP = AVG( STD_CUM_UNIT_COST ),
					@TEST_VND_CUM_UNIT_COST_PER_STEP = AVG( VND_CUM_UNIT_COST ),
					@TEST_FINISHED_PART_STD_UNIT_COST_PER_STEP = AVG( STD_FINISHED_UNIT_COST ),
					@TEST_FINISHED_PART_VND_UNIT_COST_PER_STEP = AVG( VND_FINISHED_UNIT_COST )
					FROM @YIELD_COST_TABLE
					--FROM FN_YIELD_COST_REPORT() 
					WHERE PROCESSSTEPID = @FLOW_PROCESS_STEP_ID
				 	AND DATEOUT >= @DATE_OUT_START_ITERATOR
				 	AND DATEOUT < @DATE_OUT_START_ITERATOR + @STEP_SIZE

				IF @TEST_TOTAL_UNITS_PER_STEP IS NOT NULL
				BEGIN
					-- SET TOTALS	
					SELECT @TOTAL_UNITS_PER_STEP = @TOTAL_UNITS_PER_STEP + @TEST_TOTAL_UNITS_PER_STEP
					SELECT @TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP = @TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP + @TEST_FINISHED_PART_STD_UNIT_COST_PER_STEP
					SELECT @TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP = @TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP + @TEST_FINISHED_PART_VND_UNIT_COST_PER_STEP
					SELECT @TOTAL_STD_UNIT_COST_PER_STEP = @TOTAL_STD_UNIT_COST_PER_STEP + @TEST_STD_UNIT_COST_PER_STEP
					SELECT @TOTAL_VND_UNIT_COST_PER_STEP = @TOTAL_VND_UNIT_COST_PER_STEP + @TEST_VND_UNIT_COST_PER_STEP
				END
			END

			FETCH NEXT FROM @PS_FLOW_CURSOR INTO @FLOW_PROCESS_STEP_ID, @FLOW_PROCESS_STEP_TYPE_NAME 
		END
	
	    	CLOSE @PS_FLOW_CURSOR
	    	DEALLOCATE @PS_FLOW_CURSOR


		INSERT INTO @RESULTS ( STEP_FIRST_DAY, 
			                FAB_TOTAL_WAFERS_PER_STEP, FAB_TOTAL_UNITS_PER_STEP, SORT_TOTAL_UNITS_PER_STEP, 
			                ASSY_TOTAL_UNITS_PER_STEP, TEST_TOTAL_UNITS_PER_STEP, TOTAL_UNITS_PER_STEP, 

			                FAB_STD_UNIT_COST_PER_STEP, SORT_STD_UNIT_COST_PER_STEP, 
			                ASSY_STD_UNIT_COST_PER_STEP, TEST_STD_UNIT_COST_PER_STEP, 
			                TOTAL_STD_UNIT_COST_PER_STEP, 
			
			                FAB_STD_CUM_UNIT_COST_PER_STEP, SORT_STD_CUM_UNIT_COST_PER_STEP, 
			                ASSY_STD_CUM_UNIT_COST_PER_STEP, TEST_STD_CUM_UNIT_COST_PER_STEP, 
			
			                FAB_FINISHED_PART_STD_UNIT_COST_PER_STEP, SORT_FINISHED_PART_STD_UNIT_COST_PER_STEP,
			                ASSY_FINISHED_PART_STD_UNIT_COST_PER_STEP, TEST_FINISHED_PART_STD_UNIT_COST_PER_STEP,
			                TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP,
			
			                FAB_VND_UNIT_COST_PER_STEP, SORT_VND_UNIT_COST_PER_STEP, 
					ASSY_VND_UNIT_COST_PER_STEP, TEST_VND_UNIT_COST_PER_STEP, 
					TOTAL_VND_UNIT_COST_PER_STEP,
			
			                FAB_VND_CUM_UNIT_COST_PER_STEP, SORT_VND_CUM_UNIT_COST_PER_STEP, 
					ASSY_VND_CUM_UNIT_COST_PER_STEP, TEST_VND_CUM_UNIT_COST_PER_STEP, 
			
			                FAB_FINISHED_PART_VND_UNIT_COST_PER_STEP, SORT_FINISHED_PART_VND_UNIT_COST_PER_STEP,
			                ASSY_FINISHED_PART_VND_UNIT_COST_PER_STEP, TEST_FINISHED_PART_VND_UNIT_COST_PER_STEP,
			                TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP )
			
			                VALUES( @DATE_OUT_START_ITERATOR,
			                @FAB_TOTAL_WAFERS_PER_STEP, @FAB_TOTAL_UNITS_PER_STEP, @SORT_TOTAL_UNITS_PER_STEP, 
			                @ASSY_TOTAL_UNITS_PER_STEP, @TEST_TOTAL_UNITS_PER_STEP, @TOTAL_UNITS_PER_STEP, 
			                
			                @FAB_STD_UNIT_COST_PER_STEP, @SORT_STD_UNIT_COST_PER_STEP, 
			                @ASSY_STD_UNIT_COST_PER_STEP, @TEST_STD_UNIT_COST_PER_STEP, 
			                @TOTAL_STD_UNIT_COST_PER_STEP, 
			
			                @FAB_STD_CUM_UNIT_COST_PER_STEP, @SORT_STD_CUM_UNIT_COST_PER_STEP, 
			                @ASSY_STD_CUM_UNIT_COST_PER_STEP, @TEST_STD_CUM_UNIT_COST_PER_STEP, 
			
			                @FAB_FINISHED_PART_STD_UNIT_COST_PER_STEP, @SORT_FINISHED_PART_STD_UNIT_COST_PER_STEP,
			                @ASSY_FINISHED_PART_STD_UNIT_COST_PER_STEP, @TEST_FINISHED_PART_STD_UNIT_COST_PER_STEP,
			                @TOTAL_FINISHED_PART_STD_UNIT_COST_PER_STEP,
			
			                @FAB_VND_UNIT_COST_PER_STEP, @SORT_VND_UNIT_COST_PER_STEP, 
			                @ASSY_VND_UNIT_COST_PER_STEP, @TEST_VND_UNIT_COST_PER_STEP, 
			                @TOTAL_VND_UNIT_COST_PER_STEP,
			
			                @FAB_VND_CUM_UNIT_COST_PER_STEP, @SORT_VND_CUM_UNIT_COST_PER_STEP, 
			                @ASSY_VND_CUM_UNIT_COST_PER_STEP, @TEST_VND_CUM_UNIT_COST_PER_STEP, 
			
			                @FAB_FINISHED_PART_VND_UNIT_COST_PER_STEP, @SORT_FINISHED_PART_VND_UNIT_COST_PER_STEP,
			                @ASSY_FINISHED_PART_VND_UNIT_COST_PER_STEP, @TEST_FINISHED_PART_VND_UNIT_COST_PER_STEP,
			                @TOTAL_FINISHED_PART_VND_UNIT_COST_PER_STEP )

		
		-- GO TO NEXT STEP
		SELECT @DATE_OUT_START_ITERATOR = @DATE_OUT_START_ITERATOR + @STEP_SIZE
	END

	SELECT * FROM @RESULTS
END
